Don't always run doc tests for the root package
authorAlex Crichton <alex@alexcrichton.com>
Fri, 3 Oct 2014 01:57:33 +0000 (18:57 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 6 Oct 2014 23:04:07 +0000 (16:04 -0700)
When using `cargo test -p`, be sure to run only the doc tests for the package
actually being tested.

Closes #660

src/cargo/ops/cargo_clean.rs
src/cargo/ops/cargo_rustc/compilation.rs
src/cargo/ops/cargo_rustc/context.rs
src/cargo/ops/cargo_rustc/mod.rs
src/cargo/ops/cargo_test.rs
tests/test_cargo_test.rs

index cb8786a478f4c93e511a352d509a5f0f098eae97..881ebf342b0d797e3f968532f88e795847f1d262 100644 (file)
@@ -49,7 +49,7 @@ pub fn clean(manifest_path: &Path, opts: &mut CleanOptions) -> CargoResult<()> {
     let pkgs = PackageSet::new([]);
     let cx = try!(Context::new("compile", &resolve, &srcs, &pkgs, &mut cfg,
                                Layout::at(root.get_absolute_target_dir()),
-                               None));
+                               None, &pkg));
 
     // And finally, clean everything out!
     for target in pkg.get_targets().iter() {
index 2e78d0b46227a7a9a08cd23268a4cb59a34162bf..8e8ddf6957b9ad64d4a9522e0df8818999f1dfb1 100644 (file)
@@ -35,10 +35,13 @@ pub struct Compilation {
     /// Extra environment variables that were passed to compilations and should
     /// be passed to future invocations of programs.
     pub extra_env: HashMap<String, Option<String>>,
+
+    /// Top-level package that was compiled
+    pub package: Package,
 }
 
 impl Compilation {
-    pub fn new() -> Compilation {
+    pub fn new(pkg: &Package) -> Compilation {
         Compilation {
             libraries: HashMap::new(),
             native_dirs: HashMap::new(),
@@ -47,6 +50,7 @@ impl Compilation {
             tests: Vec::new(),
             binaries: Vec::new(),
             extra_env: HashMap::new(),
+            package: pkg.clone(),
         }
     }
 
index 8f5d1d3ca647f3fc7faae7c577f29fb44b83d64a..e3762012ffaf94b325e0ba760d1d4dcf1de383af 100644 (file)
@@ -38,7 +38,8 @@ pub struct Context<'a, 'b> {
 impl<'a, 'b> Context<'a, 'b> {
     pub fn new(env: &'a str, resolve: &'a Resolve, sources: &'a SourceMap,
                deps: &'a PackageSet, config: &'b mut Config<'b>,
-               host: Layout, target: Option<Layout>)
+               host: Layout, target: Option<Layout>,
+               root_pkg: &Package)
                -> CargoResult<Context<'a, 'b>> {
         let (target_dylib, target_exe) =
                 try!(Context::filename_parts(config.target()));
@@ -66,7 +67,7 @@ impl<'a, 'b> Context<'a, 'b> {
             target_exe: target_exe,
             host_dylib: host_dylib,
             requirements: HashMap::new(),
-            compilation: Compilation::new(),
+            compilation: Compilation::new(root_pkg),
         })
     }
 
index f1428c57ede1b462016ae1ba66eb9e1dbf4db579..1731c44e8cc2fc69920be66959ee3c194777d017 100644 (file)
@@ -54,7 +54,7 @@ pub fn compile_targets<'a>(env: &str, targets: &[&'a Target], pkg: &'a Package,
                            config: &'a mut Config<'a>)
                            -> CargoResult<Compilation> {
     if targets.is_empty() {
-        return Ok(Compilation::new())
+        return Ok(Compilation::new(pkg))
     }
 
     debug!("compile_targets; targets={}; pkg={}; deps={}", targets, pkg, deps);
@@ -67,7 +67,7 @@ pub fn compile_targets<'a>(env: &str, targets: &[&'a Target], pkg: &'a Package,
     });
 
     let mut cx = try!(Context::new(env, resolve, sources, deps, config,
-                                   host_layout, target_layout));
+                                   host_layout, target_layout, pkg));
     let mut queue = JobQueue::new(cx.resolve, deps, cx.config);
 
     // First ensure that the destination directory exists
index cb81216686e2c65987fa68b3c6248d0ea3d475ac..3a2b61fc337a5cfc3b1037111b1da50383ad007f 100644 (file)
@@ -42,7 +42,7 @@ pub fn run_tests(manifest_path: &Path,
 
     if options.compile_opts.env == "bench" { return Ok(None) }
 
-    let mut libs = package.get_targets().iter().filter_map(|target| {
+    let mut libs = compile.package.get_targets().iter().filter_map(|target| {
         if !target.get_profile().is_doctest() || !target.is_lib() {
             return None
         }
index 07a7656db663eaef0c816ff98807265223dc09e4..19bb943c8e339cb613b85f6b71329201d6e304c0 100644 (file)
@@ -1053,3 +1053,50 @@ test!(build_then_selective_test {
                  .arg("-p").arg("b"),
                 execs().with_status(0));
 })
+
+test!(selective_testing_with_docs {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+
+            [dependencies.d1]
+                path = "d1"
+        "#)
+        .file("src/lib.rs", r#"
+            /// ```
+            /// not valid rust
+            /// ```
+            pub fn foo() {}
+        "#)
+        .file("d1/Cargo.toml", r#"
+            [package]
+            name = "d1"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("d1/src/lib.rs", "");
+    p.build();
+
+    assert_that(p.process(cargo_dir().join("cargo")).arg("test")
+                 .arg("-p").arg("d1"),
+                execs().with_status(0)
+                       .with_stdout(format!("\
+{compiling} d1 v0.0.1 ({dir})
+{running} target[..]deps[..]d1[..]
+
+running 0 tests
+
+test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
+
+{doctest} d1
+
+running 0 tests
+
+test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
+
+", compiling = COMPILING, running = RUNNING, dir = p.url(),
+   doctest = DOCTEST).as_slice()));
+})